package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNPerssion;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.List;

public class PerssionListAdapter3 extends BaseAdapter {
    private List<YNPerssion> listItems;
    private LayoutInflater layoutinflater;
    private Context context;

    static class ViewHolder {
        private LinearLayout perssion_item;
        private SimpleDraweeView avatar;
        private TextView name;
        private TextView open;
        private TextView perssion_item_bline;
    }

    public PerssionListAdapter3(Context context, List<YNPerssion> dataSet) {
        this.listItems = dataSet;
        this.context = context;
        this.layoutinflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final YNPerssion ynPerssion = listItems.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = layoutinflater.inflate(R.layout.perssionlist_listitem3, null);
            holder.perssion_item = (LinearLayout) convertView.findViewById(R.id.perssion_item);
            holder.avatar = (SimpleDraweeView) convertView.findViewById(R.id.perssion_item_avatar);
            holder.name = (TextView) convertView.findViewById(R.id.perssion_item_name);
            holder.open = (TextView) convertView.findViewById(R.id.perssion_item_open);
            holder.perssion_item_bline = (TextView) convertView.findViewById(R.id.perssion_item_bline);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ImageOptHelper.showFrescoAvatar(Uri.parse(ynPerssion.getUpic()), holder.avatar, context);
        holder.open.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.sendBroadcast(new Intent(ACTIONs.actionsharePerssionList));
            }
        });
        holder.name.setText(ynPerssion.getUnickname());
        if (position == listItems.size() - 1) {
            holder.perssion_item_bline.setVisibility(View.GONE);
        } else {
            holder.perssion_item_bline.setVisibility(View.VISIBLE);
        }
        return convertView;
    }


}